//生成 据点模板

//名字：营地被发现时的显示
//等级：影响守卫的强弱和数量，会提高游戏常规刷兵的等级 1-6
//阵营：0强盗 1游击队 2AAF 3NATO 4CSAT 5CIV
//图标：地图上显示的标记类型
//占地面积：周围空地的需求（会除掉树木与草等）
//最小相邻：与其他点的最小相邻距离
//激活距离：玩家距离多少米创建
//发现距离：玩家距离多少米发现，并呼叫支援
//支援时间：过这么多秒后创建额外支援
//支援等级：支援兵的强弱
//支援距离：支援部队刷新距离
//奖励等级：给予补给的等级 -2 -1 0-4
//建筑模板: 具体建筑
//固定守卫：特殊单位

params["_name", "_level", "_type", "_logo", "_disArea", "_disMutex", "_disCreate", "_disFind", "_timeSupport", "_levelSupport","_disSupport","_levelSupply", "_arrVeh", "_arrUnit", ["_levelE", 2]];
private["_pos_mk", "_marker", "_init", "_end", "_time", "_failed"];

_failed = false;


if(typeName _name == "STRING") then
{
	if(_levelE == 0) then
	{//平民点单独拿出来
		//寻找空地//60秒内不生成就放弃
		_time = time + 60;
		_pos_mk = [as_map_x + random as_map_w, as_map_y + random as_map_h];
		while{!([_pos_mk, _disArea] call fn_checkPos) || {[_pos_mk, _disMutex, "mk_e0"] call fn_checkNearMarker}}do
		{
			_pos_mk = [as_map_x + random as_map_w, as_map_y + random as_map_h];
			if(time > _time) exitWith{_failed = true};
		};
		//创建标记
		if(!_failed) then
		{
			as_marker_count = as_marker_count + 1;
			_marker = createMarker [format["mk_e0_%1", as_marker_count], _pos_mk];
		};
		
	}
	else
	{
		//寻找空地
		private _disMin = 500;
		if(_level > 2)then{
			_disMin = _level * 500 * as_near_scale;
		};
		
		_time = time + 30;
		_pos_mk = [as_map_x + random as_map_w, as_map_y + random as_map_h];
		while{!([_pos_mk, _disArea] call fn_checkPos) 
		|| {[_pos_mk, _disMutex, "mk_"] call fn_checkNearMarker}
		|| {_pos_mk distance2D as_start_pos < _disMin}}do
		{
			_pos_mk = [as_map_x + random as_map_w, as_map_y + random as_map_h];
			if(time > _time) exitWith{_failed = true};
		};
		//创建标记
		if(!_failed) then
		{
			as_marker_count = as_marker_count + 1;
			_marker = createMarker [format["mk_e_%1", as_marker_count], _pos_mk];
		};
	};
	

	//设置标记
	if(Param_ShowMarker > 0
	&& {!_failed}) then
	{
		_marker setMarkerShapeLocal "ICON";
		_marker setMarkerColorLocal "ColorRed";
		if(Param_ShowMarker == 1) then
		{	
			//有30几率不显示
			if(random 100 < 70) then
			{
				_marker setMarkerType "hd_dot";
			};
		}
		else
		{
			_marker setMarkerTypeLocal _logo;
			//设置颜色
			switch (_type) do
			{
				case 0:{_marker setMarkerColor "ColorGreen"};
				case 1:{_marker setMarkerColor "ColorGreen"};
				case 2:{_marker setMarkerColor "ColorGreen"};
				case 3:{_marker setMarkerColor "ColorBlue"};
				case 4:{_marker setMarkerColor "ColorRed"};
				case 5:{_marker setMarkerColor "ColorWhite"};
				default {_marker setMarkerColor "ColorBlack"};
			};
		};

		//据点统计
		as_num_e_num_all = as_num_e_num_all + 1;
		publicVariable "as_num_e_num_all";		
		
		//调试显示等级
		if(Param_Debug == 1) then
		{
			_marker setMarkerTextLocal format["%1", _name];
		};
	};
}
else
{
	_pos_mk = _name;
	_name = nil;
	_name = "";
	//创建标记
	as_marker_count = as_marker_count + 1;
	_marker = createMarker [format["mk_e_%1", as_marker_count], _pos_mk];
};

if(_failed) exitWith{};


[_marker, _pos_mk, _name, _disCreate, _disFind, _arrVeh, _logo, _level, _timeSupport, _levelSupport,_disSupport, _type, _levelSupply, _arrUnit, _disArea, _levelE] spawn
{
	params ["_marker", "_pos_mk", "_name", "_disCreate", "_disFind", "_arrVeh", "_logo", "_level", "_timeSupport","_levelSupport", "_disSupport", "_type", "_levelSupply", "_arrUnit", "_disArea", "_levelE"];
	private["_init", "_end", "_player", "_group", "_groupSupport", "_box"];
	
	
	//等待玩家进入
	_init = false;
	_showed = false;
	while{!_init} do
	{
		uiSleep 7;
		//没有显示，并且npc死亡或者上校被查看且为E4
		if(!_showed && {as_npc_dead || {as_map_viewed && _levelE == 4}}) then
		{	
			_marker setMarkerShapeLocal "ICON";
			_marker setMarkerTypeLocal _logo;
			_marker setMarkerDirLocal 0;
			
			//设置颜色
			switch (_type) do
			{
				case 0:{_marker setMarkerColor "ColorGreen"};
				case 1:{_marker setMarkerColor "ColorGreen"};
				case 2:{_marker setMarkerColor "ColorGreen"};
				case 3:{_marker setMarkerColor "ColorBlue"};
				case 4:{_marker setMarkerColor "ColorRed"};
				case 5:{_marker setMarkerColor "ColorWhite"};
				default {_marker setMarkerColor "ColorBlack"};
			};
			_showed = true;
		};
		
		//上校死了就不再激活点（平民和红军点除外）
		if(as_npc_dead && {_type < 4}) exitWith{deleteMarker _marker;};
		
		{
			if(((_x distance2D _pos_mk) < _disCreate)
				&& {speed _x < 200}) exitWith
			{
				_init = true;
				
				//_____________________________清障碍物_____________________________
				[_pos_mk, _disArea * 1.414 + 10] call fn_cleanupTerrain;
				
				//_____________________________创建box_____________________________
				_box = "CargoNet_01_box_F" createVehicle _pos_mk;
				_box setVariable ["marker_name", _marker];//设置标记名字方便删除
				_box enableDynamicSimulation true;
				
				[_box, _levelSupply] spawn fn_createSupply;
				
				//_____________________________创建守卫_____________________________
				if(_type != 5) then //平民不创建守卫
				{
					if(_levelE != 99
					&& {_levelE != 100})then{
						if(_type != 4)then{
							_group = [_pos_mk, 2.5, _level, 0, _type] call fn_createGroup;
						}else{//红军点ai变少
							_group = [_pos_mk, 0.25, _level, 0, _type] call fn_createGroup;
						};
						(leader _group) enableIRLasers true;
					}else{//上校、上校防卫，创建更多守卫
						_group = [_pos_mk, 3.0, _level, 0, _type] call fn_createGroup;
						(leader _group) enableIRLasers true;
					};
				}
				else
				{
					_group = createGroup civilian;
				};
				//由于后面马上需要group，需要使用call
				
				//_____________________________创建veh_____________________________
				private _veh_house = [];
				private _arrVeh2 = [];
				//载具（或者是箱子、沙袋）最后创建
				//Bunker不最后创建，因为它上面会放迫击炮
				{	
					private _type_x = (_x select 0);
					if(_type_x isKindof "AllVehicles"
					|| {_type_x find "Box" >= 0}
					|| {_type_x find "BagFence" >= 0}
					|| {_type_x find "SandbagBarricade" >= 0}) then//|| {_type_x find "BagBunker" >= 0}
					{
						//舰载武器禁用
						if(Param_E_System == 1
						|| {!(_type_x in as_veh_disable)}) then
						{
							_arrVeh2 pushback _x;
						};
					}
					else
					{
						if(count _x == 4) then
						{//只有4个参数，说明没有损坏值和油量
							_x pushback 0;
							_x pushback 1;
						};
						_x pushback _pos_mk;
						_x pushback _group;
						//
						_veh_house pushback (_x call fn_createVeh);
					};
				
				}forEach _arrVeh;
				{	
					if(count _x == 4) then
					{//只有4个参数，说明没有损坏值和油量
						_x pushback 0;
						_x pushback 1;
					};
					_x pushback _pos_mk;
					_x pushback _group;
					_x call fn_createVeh;
				}forEach _arrVeh2;
				//房屋创建固定武器（平民与红军不生成）
				if(_type != 4
				&& {_type != 5}
				&& {Param_E_Static_Add > 0}) then
				{	
					{
						if(_x isKindOf "Building") then
						{
							[_x, _group] call fn_buildingStatic;
						};
					}forEach _veh_house;
				};
				//_____________________________创建固定单位_____________________________
				{	
					private _unit_china = [_x, _pos_mk, 3, _group] call fn_createUnit;
					if(_type == 4) then
					{
						[_unit_china, "PLA"] call BIS_fnc_setUnitInsignia;
					};
				}forEach _arrUnit;
				
				//启用动态模拟
				//_group enableDynamicSimulation true;
				//发现玩家后变为战斗
				_group setBehaviour "SAFE";//"CARELESS";
				
				//寻找附近点巡逻（400-1800米之间,50%概率）
				if(random 100 < 50) then
				{
					private _mk_list = allMapMarkers select
					{
						(_x find "mk_e" == 0) 
						&& {getMarkerPos _x distance _pos_mk < (1800 * as_near_scale)}
						&& {getMarkerPos _x distance _pos_mk > (400 * as_near_scale)}
					};
					if(count _mk_list > 1) then
					{
						_mk_list deleteAt (_mk_list find _marker);
						[_group, getMarkerPos (selectRandom _mk_list)] call fn_addWaypointCycle;
					};
				};
				
				//_____________________________不被清理标记_____________________________
				_group setVariable["no_cleanup", true];
				
				//_____________________________概率刷新地图______________________________
				if((random 100 < 20) || {as_num_e_num_hold > 1 }) then//占点超过1，就必然有
				{
					private _pos_temp_map = (_pos_mk getPos [-0.7, 0]);
					private _veh  = "Land_MapBoard_01_Wall_Altis_F" createVehicle _pos_temp_map;
					_veh enableDynamicSimulation true;
					_veh setPos _pos_temp_map;
				};
				sleep 1;
				//_____________________________概率刷电脑______________________________
				if(Param_SupportSys == 1) then
				{
					private _pos_temp_map = (_pos_mk getPos [0, 0]);
					_pos_temp_map = _pos_temp_map vectorAdd [0, 0, 1.2];
					private _veh  = "Land_PCSet_01_screen_F" createVehicle _pos_temp_map;
					_veh enableDynamicSimulation true;
					_veh setPos _pos_temp_map;
					//设置纹理
					_veh setObjectTextureGlobal  [0, format["images\story\b%1.paa", (floor random 7) + 1]];
					//添加交互（all）
					//[_veh, ["<t color=""#00ff00"">联系支援</t>", "_this call fn_addSupport;", [], 8, false, true, "", ""]] remoteExec ["addAction", east, true];
					//jip只能用于0和负数（貌似还是没有生效）
					[_veh] remoteExec ["T8L_fnc_addActionLaptop", east];
					_veh spawn{
						private _player = objNull;
						waitUntil{sleep 5;_player = _this getVariable [ "T8L_pvar_player", objNull];!isNull _player};
						[_this, _player] remoteExec ["fn_addSupport", _player];
						//[_this, _player] call fn_addSupport;						
					};
				};
				
				//标记上校点已创建
				if(_levelE == 99)then{ as_end_created = true};
			};
		}forEach AllPlayers;//所有玩家都能触发点
	};
	
	
	//上校死了就不再激活点（平民和红军点除外）
	if(as_npc_dead && {_type < 4}) exitWith{};
	
	//等待玩家发现
	_end = false;
	private _mk_pos = getMarkerPos _marker;
	while{!_end} do
	{	
		{
		//加入距离限制判断
			if(_type == 5) then
			{
				if (((_x distance2D _mk_pos) < 300)) exitWith
					{
						_marker setMarkerShape "ICON";
						_marker setMarkerType _logo;
						_marker setMarkerDir 0;
						_end = true;
						if !(_name == "") then{[_x, _name] remoteExec ["sideChat"]};
						_player = _x;
					};
				}
			else
			{
				if (((_x distance2D _mk_pos) < 1000) && {(_group knowsAbout _x) > 1.49}) exitWith
				{
					_marker setMarkerShape "ICON";
					_marker setMarkerType _logo;
					_marker setMarkerDir 0;
					_end = true;
					if !(_name == "") then{[_x, _name] remoteExec ["sideChat"]};
					_player = _x;
					_group setBehaviour "COMBAT";
				};
			};
			
		}forEach (call fn_getPlayerRed);
		sleep 20;
	};
	
	//设置颜色
	switch (_type) do
	{
		case 0:{_marker setMarkerColor "ColorGreen"};
		case 1:{_marker setMarkerColor "ColorGreen"};
		case 2:{_marker setMarkerColor "ColorGreen"};
		case 3:{_marker setMarkerColor "ColorBlue"};
		case 4:{_marker setMarkerColor "ColorRed"};
		case 5:{_marker setMarkerColor "ColorWhite"};
		default {_marker setMarkerColor "ColorBlack"};
	};
	
	//靠特别近代表被玩家占领
	[_marker]spawn
	{
		params["_marker"];
		private _end = false;
		while {!_end} do
		{
			sleep 45;
			{
				if(_x distance (getMarkerPos _marker) < 3) exitWith
				{
					_end = true;
				};
			}forEach (call fn_getPlayerRed);
		};
		_marker setMarkerColor "ColorGrey";
		as_num_e_num_hold = as_num_e_num_hold + 1;
		publicVariable "as_num_e_num_hold";
	};
	
	as_level = as_level + _level;
	//被发现后 创建支援
	//红军和平民的支援改为蓝军
	if(_type == 4 || _type == 5) then{_type = 3};
	

	//突击队
	[0, selectRandom asg_easy, getPos _player, 800, 0, 1] spawn fn_createGN;
	[getPos _player, 1.0] spawn fn_aiNeedSupport;
	
	
	//呆了太久
	sleep (180 + _timeSupport * Param_EnemySupportTime + (count (call fn_getPlayerRed)) * 15 + _level * 60);
	{
		if((_x distance2D _mk_pos) < 320) exitWith
		{
			[0, selectRandom asg_normal, getPos _x, 1000, 0, 1] spawn fn_createGN;
			[getPos _x, 1.5] spawn fn_aiNeedSupport;
		};
	}forEach (call fn_getPlayerRed);
	
	//呆了更久
	sleep (240 +_timeSupport * Param_EnemySupportTime + (count (call fn_getPlayerRed)) * 20 + _level * 60);
	{
		if((_x distance2D _mk_pos) < 320) exitWith
		{
			if(random 100 < 25) then
			{
				[0, asg_10, getPos _x, 1200, 0, 1] spawn fn_createGN;
			}
			else
			{
				[0, selectRandom asg_normal, getPos _x, 1000, 0, 1] spawn fn_createGN;
			};
			[getPos _x, 2] spawn fn_aiNeedSupport;
		};
	}forEach (call fn_getPlayerRed);
	
	
};
	
